home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / gms_dev.lha / GMS / Source / C / 3DObjects / Sphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-09  |  4.0 KB  |  160 lines

  1. /*
  2. ** Name:      Spinning Sphere
  3. ** Version:   1.0
  4. ** Author:    Paul Manias
  5. ** Copyright: DreamWorld Productions (c) 1997.  Freely distributable.
  6. ** SAS/C:     sc Sphere.c
  7. **
  8. ** Doc:       This is a demo of a spinning sphere consisting entirely of dots.
  9. **            The object is pre-calculated then rotated in real time for speed,
  10. **            although things could be faster than this.
  11. **
  12. */
  13.  
  14. #include <proto/games.h>
  15. #include <math.h>
  16.  
  17. extern struct GMSBase *GMSBase;
  18. APTR   PREFSNAME = DEFAULT;
  19.  
  20. struct GameScreen *screen;
  21.  
  22. void Demo(void);
  23.  
  24. struct DotPixel { double X,Y,Z; };
  25.  
  26. #define AMTCOLOURS 32
  27.  
  28. ULONG palette[AMTCOLOURS] = {
  29.   0x000000,0x101010,0x171717,0x202020,0x272727,0x303030,0x373737,0x404040,
  30.   0x474747,0x505050,0x575757,0x606060,0x676767,0x707070,0x777777,0x808080,
  31.   0x878787,0x909090,0x979797,0xa0a0a0,0xa7a7a7,0xb0b0b0,0xb7b7b7,0xc0c0c0,
  32.   0xc7c7c7,0xd0d0d0,0xd7d7d7,0xe0e0e0,0xe0e0e0,0xf0f0f0,0xf7f7f7,0xffffff
  33. };
  34.  
  35. /***********************************************************************************/
  36.  
  37. void main(void)
  38. {
  39.   if (screen = AddScreenTags(TAGS_GAMESCREEN,NULL,
  40.      GSA_AmtColours, AMTCOLOURS,
  41.      GSA_Palette,    palette,
  42.      GSA_Attrib,     DBLBUFFER,
  43.      TAGEND)) {
  44.  
  45.      ShowScreen(screen);
  46.      InitJoyPorts();
  47.  
  48.      Demo();
  49.  
  50.   DeleteScreen(screen);
  51.   }
  52. }
  53.  
  54. /************************************************************************************
  55. ** Longtitude deterimines the position of the dot on the horizontal axis.
  56. ** Latitude determines the position of the dot on the vertical axis.
  57. */
  58.  
  59. #define AMTDOTS 500       /* The amount of dots in the object. */
  60.  
  61. void Demo(void)
  62. {
  63.   struct DotPixel *object; /* Pointer to our 3D object */
  64.   double *sine;            /* Pointer to our sine table */
  65.   double *cosine;          /* Pointer to our cosine table */
  66.   WORD   i;
  67.   WORD   offsetx = (screen->ScrWidth/2);
  68.   WORD   offsety = (screen->ScrHeight/2);
  69.   double temp;
  70.   double angle=0;
  71.   ULONG  colour;
  72.   double Z2,X2,Y2;
  73.   ULONG  jport1;
  74.   LONG   scale=32;
  75.   UWORD  anglex=0,angley=0,anglez=0;
  76.   double longtitude;
  77.   double latitude;
  78.  
  79.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_ANY);
  80.   sine   = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  81.   cosine = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  82.  
  83.   /* First calculate the X, Y and Z coordinates of our object */
  84.  
  85.   for (i=0; i<AMTDOTS; i++) {
  86.     longtitude = FastRandom(100);
  87.     latitude   = FastRandom(100);
  88.     object[i].X  = sin(longtitude)*cos(latitude);
  89.     object[i].Y  = sin(longtitude)*sin(latitude);
  90.     object[i].Z  = cos(longtitude);
  91.   }
  92.  
  93.   /* Now generate our cosine and sinus tables */
  94.  
  95.   for (i=0; i<360; i++) {
  96.     cosine[i] = cos(angle);
  97.     sine[i]   = sin(angle);
  98.     angle    += 0.25;
  99.   }
  100.  
  101.   /* Commence our main loop */
  102.  
  103.   do
  104.   {
  105.     jport1 = ReadJoyPort(JPORT1,JT_ZBXY);
  106.     scale += GetY(jport1);
  107.     if (scale < 1) scale = 1;
  108.     if (scale > 100) scale = 100;
  109.  
  110.     ClearBitmap(screen->Bitmap);
  111.  
  112.     for (i=0; i<AMTDOTS; i++) {
  113.  
  114.       X2 = object[i].X;
  115.       Y2 = object[i].Y;
  116.       Z2 = object[i].Z;
  117.  
  118.       /* Rotate the X axis */
  119.  
  120.       temp = Z2;
  121.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  122.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  123.  
  124.       /* Rotate the Y axis */
  125.  
  126. //      temp = Z2;
  127. //      Z2 = Z2*cosine[angley] - X2*sine[angley];
  128. //      X2 = X2*cosine[angley] + temp*sine[angley];
  129.  
  130.       /* Rotate the Z axis */
  131.  
  132. //      temp = X2;
  133. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  134. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  135.  
  136.       /* Calculate colour based on Z position (-1 < Z < +1) */
  137.  
  138.       colour = ((Z2+1)*screen->AmtColours)/2;
  139.  
  140.       /* Finally scale the (x,y) coordinates to enlarge or shrink the object */
  141.  
  142.       X2 *= scale;
  143.       Y2 *= scale;
  144.  
  145.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  146.     }
  147.     anglex++; if (anglex >= 360) anglex = 0;
  148.     angley++; if (angley >= 360) angley = 0;
  149.     anglez++; if (anglez >= 360) anglez = 0;
  150.  
  151.     WaitVBL();
  152.     SwapBuffers(screen);
  153.   } while(!(jport1 & MB_LMB));
  154.  
  155.   FreeMemBlock(object);
  156.   FreeMemBlock(sine);
  157.   FreeMemBlock(cosine);
  158. }
  159.  
  160.